import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xiaofanshu_flutter/utils/guide_service.dart';

/// 引导功能调试助手
class GuideDebugHelper {
  /// 检查引导状态
  static Future<Map<String, dynamic>> checkGuideStatus() async {
    final status = {
      'isFirstLaunch': await GuideService.isFirstLaunch(),
      'isGuideCompleted': await GuideService.isGuideCompleted(),
      'isHomeGuideCompleted': await GuideService.isHomeGuideCompleted(),
      'isStoryMapGuideCompleted': await GuideService.isStoryMapGuideCompleted(),
      'isMessageGuideCompleted': await GuideService.isMessageGuideCompleted(),
      'isMineGuideCompleted': await GuideService.isMineGuideCompleted(),
    };
    
    print('=== 引导状态检查 ===');
    status.forEach((key, value) {
      print('$key: $value');
    });
    print('==================');
    
    return status;
  }
  
  /// 重置所有引导状态（用于测试）
  static Future<void> resetAllGuides() async {
    await GuideService.resetGuide();
    print('✅ 所有引导状态已重置');
  }
  
  /// 显示调试对话框
  static void showDebugDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('引导功能调试'),
        content: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                '调试工具',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
              const SizedBox(height: 16),
              _buildDebugButton(
                context,
                '检查引导状态',
                () async {
                  await checkGuideStatus();
                  Get.snackbar('提示', '请查看控制台日志');
                },
              ),
              const SizedBox(height: 8),
              _buildDebugButton(
                context,
                '重置所有引导',
                () async {
                  await resetAllGuides();
                  Get.snackbar(
                    '成功',
                    '引导状态已重置，请重启应用',
                    backgroundColor: Colors.green,
                    colorText: Colors.white,
                  );
                },
              ),
              const SizedBox(height: 8),
              _buildDebugButton(
                context,
                '强制显示欢迎对话框',
                () async {
                  Get.back();
                  await Future.delayed(const Duration(milliseconds: 300));
                  _forceShowWelcomeDialog(context);
                },
              ),
              const SizedBox(height: 16),
              const Text(
                '说明：',
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 8),
              const Text(
                '1. 检查引导状态：查看当前引导的完成情况\n'
                '2. 重置所有引导：清除引导记录，重启后会重新显示\n'
                '3. 强制显示：无论状态如何都显示欢迎对话框',
                style: TextStyle(fontSize: 12),
              ),
            ],
          ),
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: const Text('关闭'),
          ),
        ],
      ),
    );
  }
  
  static Widget _buildDebugButton(
    BuildContext context,
    String label,
    VoidCallback onPressed,
  ) {
    return SizedBox(
      width: double.infinity,
      child: ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: const Color(0xFF6366F1),
          foregroundColor: Colors.white,
        ),
        child: Text(label),
      ),
    );
  }
  
  /// 强制显示欢迎对话框
  static void _forceShowWelcomeDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => AlertDialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        ),
        title: Row(
          children: [
            Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: const Color(0xFF6366F1).withOpacity(0.1),
                borderRadius: BorderRadius.circular(12),
              ),
              child: const Icon(
                Icons.waving_hand,
                color: Color(0xFF6366F1),
                size: 28,
              ),
            ),
            const SizedBox(width: 12),
            const Expanded(
              child: Text(
                '欢迎来到故事地图',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '这是调试模式的欢迎对话框！',
              style: TextStyle(
                fontSize: 15,
                color: Color(0xFF6B7280),
              ),
            ),
            const SizedBox(height: 12),
            const Text(
              '功能介绍：',
              style: TextStyle(
                fontSize: 14,
                color: Color(0xFF6B7280),
              ),
            ),
            const SizedBox(height: 16),
            _buildFeatureItem(Icons.home, '浏览精彩故事'),
            _buildFeatureItem(Icons.map, '在地图上探索'),
            _buildFeatureItem(Icons.message, '查看消息互动'),
            _buildFeatureItem(Icons.person, '管理个人信息'),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () {
              Get.back();
            },
            child: const Text(
              '关闭',
              style: TextStyle(color: Color(0xFF9CA3AF)),
            ),
          ),
          ElevatedButton(
            onPressed: () {
              Get.back();
              Get.snackbar(
                '提示',
                '这是测试模式，实际引导需要在首次启动时显示',
                backgroundColor: const Color(0xFF6366F1),
                colorText: Colors.white,
              );
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: const Color(0xFF6366F1),
              foregroundColor: Colors.white,
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
            ),
            child: const Text(
              '确定',
              style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
            ),
          ),
        ],
      ),
    );
  }
  
  static Widget _buildFeatureItem(IconData icon, String text) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        children: [
          Icon(icon, size: 20, color: const Color(0xFF6366F1)),
          const SizedBox(width: 12),
          Text(
            text,
            style: const TextStyle(fontSize: 14, color: Color(0xFF374151)),
          ),
        ],
      ),
    );
  }
}

